package edu.scnu.fans.fingertip.user.controller;

import edu.scnu.fans.fingertip.common.api.ApiResponse;
import edu.scnu.fans.fingertip.common.api.HeaderConstant;
import edu.scnu.fans.fingertip.common.api.ResponseUtils;
import edu.scnu.fans.fingertip.user.api.UserApi;
import edu.scnu.fans.fingertip.user.input.LoginRegisterInput;
import edu.scnu.fans.fingertip.user.output.UserOutput;
import edu.scnu.fans.fingertip.user.output.VerifyTokenOutput;
import edu.scnu.fans.fingertip.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "用户API")
@RestController
public class UserController implements UserApi {

    @Resource
    private UserService userService;

    @ApiOperation("登陆")
    @PostMapping("/fingertip/user/v1/login")
    public ApiResponse<UserOutput> login(@Valid @RequestBody LoginRegisterInput input) {
        UserOutput output = userService.login(input);
        return new ApiResponse<>(output);
    }

    @ApiOperation("注册")
    @PostMapping("/fingertip/user/v1/register")
    public ApiResponse<UserOutput> register(@Valid @RequestBody LoginRegisterInput input) {
        UserOutput output = userService.register(input);
        return new ApiResponse<>(output);
    }

    @ApiOperation("个人资料")
    @PostMapping("/fingertip/user/v1/info")
    public ApiResponse<UserOutput> info(@RequestHeader(HeaderConstant.USER_ID) Long userId) {
        UserOutput output = userService.findUserById(userId);
        return new ApiResponse<>(output);
    }

    @ApiOperation("登出")
    @PostMapping("/fingertip/user/v1/logout")
    public ApiResponse<?> logout(@RequestHeader(HeaderConstant.TOKEN) String token) {
        userService.logout(token);
        return ResponseUtils.EMPTY;
    }

    @Override
    @ApiOperation("验证token")
    @PostMapping("/fingertip/user/v1/verifyToken")
    public ApiResponse<VerifyTokenOutput> verifyToken(@RequestHeader(HeaderConstant.USER_ID) Long userId,
                                                      @RequestHeader(HeaderConstant.TOKEN) String token) {
        VerifyTokenOutput output = userService.verifyToken(userId, token);
        return new ApiResponse<>(output);
    }

}
